package cloud.smalla.service.impl;


import cloud.smalla.entity.User;
import cloud.smalla.exception.AuthException;
import cloud.smalla.mapper.UserMapper;
import cloud.smalla.vo.LoginUser;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.omg.CORBA.SystemException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * springSecurity中的身份和权限认证
 *
 * @author 曾
 * Time：2023-06-27 13:56
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    /**
     * 重写登录验证的方法
     * @param username 用户的账号
     * @return 返回UserDetails
     * @throws UsernameNotFoundException 异常
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //1.根据用户名查询用户信息
        User user = userMapper.getUserById(username);

        //2.如果查询不到就抛出异常
        if(ObjectUtils.isNull(user)){
            throw new AuthException(403,"未实现登录");
        }

        //TODO 身份信息写这(查询数据库)
        List<String> permissions = new ArrayList<>();
        //返回UserDetail
        return new LoginUser(user,permissions);
    }
}
